package com.tanhua.dubbo.api.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.enums.SexEnum;
import com.tanhua.dubbo.mapper.UserInfoMapper;
import com.tanhua.dubbo.pojo.UserInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@DubboService(version = "1.0.0")
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public Boolean save(UserInfo userInfo) {
        //校验对象中的数据是否符合要求
        if (!ObjectUtil.isAllNotEmpty(userInfo.getUserId(), userInfo.getNickName())) {
            return false;
        }

        userInfo.setAge(20);//默认值
        userInfo.setEdu("本科");//默认值
        userInfo.setIncome("40");//默认值
        userInfo.setIndustry("计算机");//默认值
        userInfo.setMarriage("未婚");//默认值
        userInfo.setTags("单身,本科,年龄相仿");//默认值
        return this.userInfoMapper.insert(userInfo) == 1;
    }

    @Override
    public Boolean update(UserInfo userInfo) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userInfo.getUserId()); //条件
        return this.userInfoMapper.update(userInfo, queryWrapper) == 1;
    }

    @Override
    public UserInfo queryByUserId(Long userId) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId); //条件
        return this.userInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public List<UserInfo> queryByUserIdList(List<Object> userIdList) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIdList); //条件
        return this.userInfoMapper.selectList(queryWrapper);
    }

    @Override
    public Map<Long, UserInfo> queryMapByUserIdList(List<Object> userIdList) {
        List<UserInfo> userInfoList = this.queryByUserIdList(userIdList);
        return userInfoList.stream()
                .collect(Collectors.toMap(UserInfo::getUserId, userInfo -> userInfo));
    }

    /**
     * 模糊搜索
     * @param userIdList
     * @param nickname
     * @return
     */
    @Override
    public Map<Long, UserInfo> queryMapByUserIdListAndNikeName(List<Object> userIdList,String nickname) {
        List<UserInfo> userInfoList = this.queryByUserIdListAndNikeName(userIdList,nickname);
        return userInfoList.stream()
                .collect(Collectors.toMap(UserInfo::getUserId, userInfo -> userInfo));
    }

    /**
     * 模糊搜索
     * @param userIdList
     * @param nickname
     * @return
     */
    @Override
    public List<UserInfo> queryByUserIdListAndNikeName(List<Object> userIdList,String nickname) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIdList); //条件
        if (StrUtil.isNotEmpty(nickname)){
            queryWrapper.like("nick_name", nickname); //模糊查询
        }
        return this.userInfoMapper.selectList(queryWrapper);
    }

    @Override
    public List<UserInfo> queryListLikeNickName(List<Object> userIdList, String keyword) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIdList); //条件
        queryWrapper.like("nick_name", keyword); //模糊查询
        return this.userInfoMapper.selectList(queryWrapper);
    }

    @Override
    public List<UserInfo> queryListByGender(List<Object> userIdList, String gender) {
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        //用户id
        queryWrapper.in("user_id", userIdList);
        if (StrUtil.isNotEmpty(gender)) {
            //性别
            queryWrapper.eq("sex", (ObjectUtil.equal(gender, "man")) ? SexEnum.MAN.getValue() : SexEnum.WOMAN.getValue());
        }
        return this.userInfoMapper.selectList(queryWrapper);
    }

    //根据条件查询用户信息集合
    @Override
    public List<UserInfo> queryByRecommendUser(List<Object> userIdList, Integer age, String city) {
        //添加过滤条件
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", userIdList);
        if (age != null) {
            //年龄小于等于目标值
            queryWrapper.le("age", age);
        }
        if (ObjectUtil.isNotEmpty(city)) {
            //城市 like 操作
            queryWrapper.like("city", city);
        }


        return this.userInfoMapper.selectList(queryWrapper);
    }
}
